
#include <fal.h>
#include <rtdevice.h>
#include <rtthread.h>
#include <string.h>
#include <unistd.h>
#include "extern_flash_read.h"
#include "uc_log.h"

/**
 * @brief 读取外部flash中ap镜像. ICP工具进行bin文件烧录时的地址偏移为 0x90600000(0x90000000 + 6M)
 *
 * @ffset: 偏移，最大为 1.2M(1228800),
 * @buf: 读出的数据存放地址,
 * @len: 读出的数据长度
 *
 */
int read_ap_param(int offset, char *buf, int len)
{
    int length;
    const struct fal_partition *ap_part = RT_NULL;

    if (RT_NULL == ap_part)
    {
        ap_part = fal_partition_find(AP_PARAMENT_NAME);
        if (ap_part == NULL)
        {
            TRACE_E("ap partition not find");
            return 1;
        }
    }
    length = fal_partition_read(ap_part, ap_part->len - AP_PARAMENT_SIZE + offset, buf, len);
    if (length <= 0)
    {
        TRACE_E("ap partition read err.");
        return 2;
    }

    return 0;
}

/**
 * @brief 读取外部flash中同步助手镜像. ICP工具进行bin文件烧录时的地址偏移为 0x9072C000 (0x90000000 + 6M + 1200K)
 *
 * @ffset: 偏移，最大为 400K(409600),
 * @buf: 读出的数据存放地址,
 * @len: 读出的数据长度
 *
 */
int read_200t_param(int offset, char *buf, int len)
{
    int length;
    const struct fal_partition *u200t_part = RT_NULL;

    if (RT_NULL == u200t_part)
    {
        u200t_part = fal_partition_find(U200T_PARAMENT_NAME);
        if (u200t_part == NULL)
        {
            TRACE_E("200t partition not find");
            return 1;
        }
    }
    length = fal_partition_read(u200t_part, u200t_part->len - U200T_PARAMENT_SIZE + offset, buf, len);
    if (length <= 0)
    {
        TRACE_E("200t partition read err.");
        return 2;
    }

    return 0;
}
